/* Exercise 4.9 Second Clock with Stop/Start and Laptime */ /* Created David Miles April 2006 */ /* Updated Ben Rowland Febuary 2014 */ #include // CONFIG1 #pragma config FOSC = HS // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config VCAPEN = OFF // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled) #pragma config PLLEN = OFF // PLL Enable (4x PLL disabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #define _XTAL_FREQ 19660800 // Defines the hardware crystal frequency allowing the delay function to work correctly //A value for each bit in PORTA. We can feed in the number of the LED and it will give us the value to put into A const unsigned char enable [4] = { 1, 2, 4, 8 } ; //These are the patterns for the LEDs which were worked out from the datasheet. //Note that to light a LED the bit on PORTB must be low I can use this array to convert from a digit to the 7 segments needed const unsigned char patterns [10] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x83, 0xf8, 0x80, 0x98 } ; // 0 1 2 3 4 5 6 7 8 9 #define DISPLAY_SIZE 4 //Number of LEDs in our display #define RUNNING 0 //State values for our stopwatch #define PAUSED 1 #define LAP_TIME 2 #define DPOINT 0x7f #define NO_DPOINT 0xff unsigned char segments [DISPLAY_SIZE] ; //Segment patterns for our LEDs unsigned char led_counter = 0 ; //Counter used by our refresh unsigned char counter = 0; //Timer interrupt counter unsigned char portbinputs; //PORTB stored input value unsigned char newportb; //new version of PORTB unsigned char oldportb; //old version of PORTB unsigned char state = PAUSED; //Stopwatch state value unsigned char old_button; unsigned char new_button; unsigned char new_lap_button; unsigned char old_lap_button; int count = 0; //Our counter value int lap_count; //Our lap count value int divisor = 0; //Interrupt divide count value void setup_hardware (void) { ANSELA = 0x00; //Set PORTA to Digital Mode - Defaults to analogue mode ANSELB = 0x00; //Set PORTB to Digital Mode - Defaults to analogue mode OPTION_REG = 0xC0; TRISA = 0xF0; //Set bits 0-3 PORTA to be configured as an output TRISB = 0x00; //Set all bits of PORTB to be configured as an output INTCON = 0b10100000; //Configure INTCON register to enable timer interrupts OPTION_REG = 0b11000100; //Configure the timer to use the prescaler 1:32 - 19660800 / 4 / 32 / 256 = 600Hz } //Each time refresh is called it sets the display for a led and moves on to the next void refresh ( void ) { LATA = 0; //Turn off all the LEDS LATB = 0; //now read PORTB, turn off the outputs first TRISB = 0xFF; //set all of PORTB for input newportb = PORTB; //copy the input pins TRISB = 0x00; //set all of PORTB for output /* now do the display update */ LATB = segments[led_counter]; //Set segments for the led LATA = enable[led_counter]; //Turn the led on led_counter = led_counter + 1 ; //Move on to the next led if ( led_counter == DISPLAY_SIZE ) //see if we fell off the end { led_counter = 0 ; } if (newportb == oldportb) //now do the debounce { portbinputs = newportb ; } oldportb = newportb; //store the old value for next time } //Display just loads the pattern into the segment. refresh will read it later void display ( unsigned char digit, unsigned char pos, unsigned char mask ) { segments[pos] = patterns[digit] & mask; } //Displays the lap time void lap_button_down ( void ) { if (state == RUNNING ) { state = LAP_TIME ; //Go into the LAP_TIME state lap_count = count ; //Save the counter value for display as the lap time return ; } if (state == LAP_TIME) //if we are in lap state go back to running again { state = RUNNING ; } //don't change state if we are in any other state } //Event handler for button down to filp the running state void button_down ( void ) { if (state == RUNNING ) //If the clock is currently running? { state = PAUSED ; //Then stop it from running } else { state = RUNNING ; //Otherwise start it running } } //TMR0 Overflow handler void tmrHandler( void ) { refresh(); if (state != PAUSED) { divisor++; if (divisor == 20) //update count every 10th of a second { count++; divisor = 0; } } new_button = portbinputs & 0x01 ; //mask off the control button using the bottom bit of PORTB if (new_button != old_button) { if (new_button) //If the new button state is button down we deliver the event by calling the button_down event handler { button_down(); } } old_button = new_button ; //remember the previous state //repeat the code for a lap button new_lap_button = portbinputs & 0x02 ; //Mask off the lap button using bit 1 of PORTB if ( new_lap_button != old_lap_button ) { if ( new_lap_button ) //if the new button state is button down we deliver the event by calling the lap_button_down event handler { lap_button_down () ; } } old_lap_button = new_lap_button ; //remember the previous state } //Allows a numeric variable to be printed to the 4 x 7-seg LEDs void display_value ( int value ) { display ( value % 10, 3, NO_DPOINT ) ; //display the units value = value / 10 ; display ( value % 10, 2, NO_DPOINT ) ; //display the tens value = value / 10 ; display ( value % 10, 1, NO_DPOINT ) ; //display the hundreds value = value / 10 ; display ( value % 10, 0, NO_DPOINT ) ; //display the thousands } //This is the interrupt handler which is called when the PIC detects an interrupt //It checks the status bits to find out who caused the interrupt and then calls that handler void interrupt isr( void ) { if(INTCON & 0b00000100) //if the timer has overflowed bit 2 of INTCON is set high { INTCON = INTCON & 0b11111011; //Clear bit 2 to turn off this interrupt counter = counter + 1; //Increment interrupt counter if ( counter > 2 ) //If counter is greater than 2 { counter = 0; //Reset counter to 0 tmrHandler(); //Call the interrupt handler function } } } int main (void) { setup_hardware(); while (1) //Loop forever { if ( portbinputs & 0x04 ) //Use the bit 2 of PORTB to reset the watch { count = 0 ; } if ( state == LAP_TIME ) { display_value (lap_count); } else { display_value (count); //Send new count to LEDs } } return 0; }